/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.freedoit.gui;

import java.awt.Color;
import javax.swing.JTextField;

/**
 *
 * @author wanglei
 */
public class TipTextFiled extends JTextField {
    private String strTip;
    private boolean blnEdited;
    
    private Color tipColor;
    private Color normalColor;
    
    public TipTextFiled () {
        this("");
    }
    
    public TipTextFiled (String strTip) {
        if (strTip == null) {
            this.strTip = "";
        } else {
            this.strTip = strTip;
        }
        
        this.blnEdited = false;
        this.tipColor = super.getDisabledTextColor();
        this.normalColor = super.getForeground();
        
        // Initialize as disable text
        super.setForeground(tipColor);
        
        // Add Event Listener
        addEventListener();
    }
    
    private void addEventListener() {
        
        this.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextFieldMouseClicked(evt);
            }
        });
        this.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldFocusLost(evt);
            }
        });
    }
    
    public void setDisabledTextColor(Color color) {
        this.tipColor = color;
        super.setDisabledTextColor(color);
    }
    
    public void setForeground(Color color) {
        this.normalColor = color;
        super.setForeground(color);
    }                                                     

    private void jTextFieldFocusGained(java.awt.event.FocusEvent evt) {                                              
        // TODO add your handling code here:
        //System.out.println("gain:" + blnEdited);
        if (!blnEdited) {
            blnEdited = true; // will not fire the document change action
            this.setText("");
            super.setForeground(normalColor);
            blnEdited = false;
        }
    }                                                  

    private void jTextFieldFocusLost(java.awt.event.FocusEvent evt) {                                            
        // TODO add your handling code here:
        //System.out.println("lost:" + blnEdited);
        if (!blnEdited) {
            blnEdited = true; // will not fire the document change action
            
            // Not Edited
            if ("".equals(this.getText())) {
                this.setText(strTip);
                super.setForeground(tipColor);
                blnEdited = false;
            }
        }
    }                                           

    private void jTextFieldMouseClicked(java.awt.event.MouseEvent evt) {                                               
        // TODO add your handling code here:
        //System.out.println("click:" + blnEdited);
        if (!blnEdited) {
            blnEdited = true; // will not fire the duplicated action of focus gained
            this.setFocusable(true);
            super.setForeground(normalColor);
            this.setText("");
            blnEdited = false;
        }
    }
}
